package com.chenchao.net;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @encoding : UTF-8
 * @Time : 2020/11/24 19:10
 * @Author : Chen Chao
 * @File : javaSE.com.chenchao.net.Test04.java
 * @Software : IntelliJ IDEA
 * @since : 1.8
 * ServerSocket套节字 模拟服务端
 */
public class Test04 {
    public static void main(String[] args) throws IOException {
        /*服务端读入*/
        /*创建一个带端口号的ServerSocket*/
        ServerSocket serverSocket = new ServerSocket(8080);
        /*获取客户端的socket*/
        Socket accept = serverSocket.accept();
        /*从客户端的输出流中读入*/
        InputStreamReader isr = new InputStreamReader(accept.getInputStream());
        char[] chars = new char[1024];
        int len;
        while ((len = isr.read(chars))!=-1){
            System.out.println(len);
            System.out.println(new String(chars,0,len));
        }

        /*服务端写出*/
        OutputStream outputStream = accept.getOutputStream();
        /*在客户端的输入流中写入*/
        outputStream.write("Hello,my dear Client!".getBytes());

        /*关闭资源*/
        accept.close();
        serverSocket.close();

    }
}
